package com.heyqing.topic.IsIsomorphic_0205;

import java.util.*;

/**
 * ClassName:IsIsomorphic
 * Package:com.heyqing.topic.IsIsomorphic_0205
 * Description:
 *
 * @Date:2024/9/22
 * @Author:Heyqing
 */
public class IsIsomorphic {
    /**
     * 可以返回其字符第一次出现的位置
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isIsomorphic(String s, String t) {
        int lenS = s.length(), lenT = t.length();
        if (lenT != lenS) return false;
        Map<Character, Character> mapS = new HashMap<>();
        Map<Character, Character> mapT = new HashMap<>();
        for (int i = 0; i < lenS; i++) {
            char cs = s.charAt(i), ct = t.charAt(i);
            if ((mapS.containsKey(cs) && mapS.get(cs) != ct) || (mapT.containsKey(ct) && mapT.get(ct) != cs)) {
                return false;
            }
            mapS.put(cs, ct);
            mapT.put(ct, cs);
        }
        return true;
    }

    public static void start() {
        Scanner sc = new Scanner(System.in);
        System.out.println("s:");
        String s = sc.nextLine();
        System.out.println("t:");
        String t = sc.nextLine();
        IsIsomorphic ii = new IsIsomorphic();
        boolean isomorphic = ii.isIsomorphic(s, t);
        System.out.println(isomorphic);
    }
}
